package com.fengmc.web;

import org.springframework.util.ReflectionUtils;
import org.springframework.web.WebApplicationInitializer;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;
import java.lang.reflect.Modifier;
import java.util.LinkedList;
import java.util.Set;

@HandlesTypes(MyWebApplicationInitializer.class)
public class MySpringServletContainerInitializer implements ServletContainerInitializer {
    @Override
    public void onStartup(Set<Class<?>> set, ServletContext servletContext) throws ServletException {
        System.out.println("Initializer MySpringServletContainerInitializer ");
        LinkedList<WebApplicationInitializer> initializers = new LinkedList<WebApplicationInitializer>();
        if (set != null) {
            for (Class<?> aClass : set) {
                if (!aClass.isInterface() && !Modifier.isAbstract(aClass.getModifiers())) {
                    WebApplicationInitializer.class.isAssignableFrom(aClass);
                    try {
                        initializers.add((WebApplicationInitializer) ReflectionUtils.accessibleConstructor(aClass).newInstance());
                    } catch (Exception e) {
                        throw new ServletException("filed to instance MyWebApplicationInitializer");
                    }
                }
            }
        }
    }
}

